package com.yafeng.logic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yafeng.comonutils.ApiResult;
import com.yafeng.logic.vo.QueryUserInfo;
import com.yafeng.logic.entity.UserOprator;
import com.yafeng.logic.mapper.UserOperatorMapper;
import com.yafeng.logic.service.UserOperatorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yafeng.logic.vo.SetRoleVo;
import com.yafeng.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cn.classbao
 * @since 2020-08-04
 */
@Service
public class UserOperatorServiceImpl extends ServiceImpl<UserOperatorMapper, UserOprator> implements UserOperatorService {

    @Autowired
    private UserOperatorMapper userOperatorMapper;

    @Value("${token.app_subject}")
    private String app_subject;

    @Value("${token.cookieTime}")
    private int cookieTime;

    @Override
    public UserOprator queryUserById(String username) {

        QueryWrapper<UserOprator> queryWrapper =new QueryWrapper<>();

        queryWrapper.eq("username",username);

        return userOperatorMapper.selectOne(queryWrapper);
    }

    @Override
    public ApiResult userQueryList(QueryUserInfo queryUserInfo) {


        List<UserOprator> userOprators = userOperatorMapper.userQueryList(queryUserInfo);
        return ApiResult.ok().data("items",userOprators).data("total",queryUserInfo.getTotal());

    }

    @Override
    public ApiResult toUserSetRole(SetRoleVo setRoleVo) throws Exception{

       //首先删除userid的相关信息
       userOperatorMapper.deleteUserIdInfo(setRoleVo.getUserId());


       for (int i = 0; i < setRoleVo.getRoleIds().length; i++) {

            userOperatorMapper.insertUserIdAndRoleId(setRoleVo.getUserId(),setRoleVo.getRoleIds()[i]);

        }

        return  ApiResult.ok();
    }

    @Override
    public String generateJwtToken(String username, long time) {

        Map<String, Object> claims = new HashMap<>();
        claims.put("username", username);
        String subject = app_subject + username;
        return JwtUtils.generateToken(subject, time, claims);
    }

    @Override
    public void generateTokenCookie(HttpServletResponse response, String newToken) throws Exception {

        if (response == null) {
            RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
            response = ((ServletRequestAttributes) requestAttributes).getResponse();
        }

        Cookie token = new Cookie("token", newToken);
        System.out.println("cookie=="+cookieTime);
        //token.setDomain("host");
        token.setMaxAge(cookieTime); //7天
        token.setPath("/");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.addCookie(token); //设置cookie
    }
}
